import React, { Component } from 'react';
import CustomForm from '../../../components/CustomForm';
import { Input, message } from 'antd';
import services from '../../../services/api';

class RiskCaseCategoryAdd extends Component {
  constructor(props) {
    super(props);
    this.state = {
      depts: []
    };
    this.actionType = 'add';
    this.form = null;
    this.api = 'caseCategory';
  }

  componentDidMount() {}

  handleSubmit = () => {
    this.form.validateFields((err, values) => {
      if (!err) {
        const params = {
          name: values.name
        };
        if (this.actionType === 'update') {
          params.id = this.props.record.case_category_id;
        }
        services[this.api][this.actionType](params).then(data => {
          if (data.code === 0) {
            message.success(data.msg);
            if (this.props.closeModal) {
              this.props.closeModal();
            }
          } else {
            message.error(data.msg);
          }
        });
      }
    });
  };

  render() {
    this.actionType = this.props.actionType;
    const { case_category_name } = this.props.record || {};
    const formItems = [
      {
        label: {
          label: '案件名称',
          className: 'tantuer-form-item3 tantuer-form-item-nowrap'
        },
        name: 'name',
        options: {
          initialValue: case_category_name,
          rules: [
            { required: true, message: '请输入案件名称' },
            { max: 128, message: '最长128个字符' },
            { pattern: /^[\u4E00-\u9FA5_a-zA-Z0-9]+$/, message: '限中英文数字下划线' }
          ]
        },
        component: <Input placeholder='案件名称' disabled={this.actionType === 'detail'} />
      }
    ].filter(Boolean);

    return (
      <CustomForm
        {...this.props}
        closeModal={() => this.props.closeModal(true)}
        createForm={ref => (this.form = ref)}
        formItems={formItems}
        onHandleSubmit={this.handleSubmit}
        colNum={1}
        hideSubmit={this.actionType === 'detail'}
      />
    );
  }
}

export default RiskCaseCategoryAdd;
